package com.maimao.spider.pipeline;

import com.maimao.spider.mapper.BrandMapper;
import com.maimao.spider.model.Brand;
import com.maimao.spider.utils.PinYinUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2019/11/2421:14
 */
@Component
@Slf4j
public class BrandPipeline implements Pipeline {

    @Autowired
    private BrandMapper brandMapper;


   /* @Override
    @Transactional(rollbackFor = Exception.class)
    public void process(ResultItems resultItems, Task task) {
        List<String>brandNames = resultItems.get("brandNames");
        List<Brand>brandList=new ArrayList<>(brandNames.size());

        Brand brand;
        for (String brandName : brandNames) {
            brand=new Brand();
            brand.setName(brandName);
            brand.setCreateTime(LocalDateTime.now());
            brand.setDesc(brandName);
            brand.setHot(0);
            brand.setOrder(1);
            brand.setRecommend(0);
            brand.setPinyin(PinYinUtils.getPingYin(brandName));
            brand.setHead(PinYinUtils.getHead(brandName));
            brandList.add(brand);
        }
        brandMapper.insertBatch(brandList);
    }*/
   @Override
   @Transactional(rollbackFor = Exception.class)
   public void process(ResultItems resultItems, Task task) {
       List<String>brandNames = resultItems.get("brandNames");
       List<Brand>brandList=new ArrayList<>(brandNames.size());

       Brand brand;
       for (String brandName : brandNames) {
           Brand select = brandMapper.selectIdByName(brandName);
           if (select!=null){
               // 203 笔记本 640 洗发水 针织衫 464 356 吸顶灯
               log.info("重复 {}-{}-{}",select.getId(),brandName,356);
               List<Long> bid = brandMapper.selectBrandIdByCateId(356);
               if (!bid.contains(select.getId())){
                   brandMapper.insertCateBrand(select.getId(),356);
               }
           }else {
               brand=new Brand();
               brand.setName(brandName);
               brand.setCreateTime(LocalDateTime.now());
               brand.setDesc(brandName);
               brand.setHot(0);
               brand.setOrder(1);
               brand.setRecommend(0);
               brand.setPinyin(PinYinUtils.getPingYin(brandName));
               brand.setHead(PinYinUtils.getHead(brandName));
               brandMapper.insertBrand(brand);
               brandMapper.insertCateBrand(brand.getId(),356);
               log.info("正常{}",brand.getName());
//               brandList.add(brand);
           }

       }
   }
}
